package com.huawei.pet.service.impl;

import com.huawei.basic.exception.BusinessException;
import com.huawei.basic.service.IBaiduAiAuditService;
import com.huawei.basic.util.LoginContext;
import com.huawei.org.domain.Employee;
import com.huawei.org.mapper.EmployeeMapper;
import com.huawei.pet.domain.Pet;
import com.huawei.pet.domain.PetDetail;
import com.huawei.pet.domain.PetOnlineAuditLog;
import com.huawei.pet.mapper.PetDetailMapper;
import com.huawei.pet.mapper.PetMapper;
import com.huawei.pet.mapper.PetOnlineAuditLogMapper;
import com.huawei.pet.service.IPetService;
import com.huawei.basic.service.impl.BaseServiceImpl;
import com.huawei.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);

        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    //下架业务
    @Override
    public void offsale(Long[] ids) {
        petMapper.offsale(ids);

    }

    //上架业务
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        //获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());


        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            String name = pet.getName();
            String resources = pet.getResources();
            Boolean textBoo = baiduAiAuditService.textAudit(name);
            Boolean imageBoo = baiduAiAuditService.imageAudit(resources);
            if (textBoo && imageBoo){//通过审核
                //2.审核通过：修改状态 + 添加上架的审核成功日志
                pet.setState(1);
                petMapper.update(pet);

                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                petOnlineAuditLogMapper.save(log);


            }else {//未通过审核
                //3.审核失败：添加上架的审核失败日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(-1);
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核失败，内容有非法数据");
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败");
            }
        }
    }

}
